﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Collections;

public static class ViewExtensions
{
    /// <summary>
    /// Enums which describe script libraries
    /// </summary>
    public enum ScriptLibrary
    {
        Ajax,
        StateDropDown
    }

    /// <summary>
    /// Types of HTML Lists
    /// </summary>
    public enum ListType
    {
        Ordered,
        Unordered,
        TableCell
    }

    /// <summary>
    /// A helper for registering script tags on an MVC View page.
    /// </summary>
    public static string RegisterJs(this HtmlHelper helper, ScriptLibrary scriptLib)
    {
        //get the directory where the scripts are
        var scriptRoot = VirtualPathUtility.ToAbsolute("~/Content/Scripts");
        var scriptFormat = "<script src=\"{0}/{1}\" type=\"text/javascript\"></script>\r\n";

        var sb = new StringBuilder();

        //all of the script tags
        if (scriptLib == ScriptLibrary.Ajax)
        {
            sb.AppendLine(RegisterScript(helper, "MicrosoftAjax.js"));
            sb.AppendLine(RegisterScript(helper, "MicrosoftAjaxMVC.js"));
        }
        else
        {
            sb.AppendLine(RegisterScript(helper, Enum.GetName(typeof(ScriptLibrary), scriptLib) + ".js"));
        }
        return sb.ToString();
    }


    /// <summary>
    /// A helper for registering script tags on an MVC View page.
    /// </summary>
    public static string RegisterScript(this HtmlHelper helper, string scriptFileName)
    {
        //get the directory where the scripts are
        var scriptRoot = VirtualPathUtility.ToAbsolute("~/Content/Scripts");
        const string scriptFormat = "<script src=\"{0}/{1}\" type=\"text/javascript\"></script>\r\n";

        return string.Format(scriptFormat, scriptRoot, scriptFileName);
    }

    /// <summary>
    /// Creates a formatted list of items based on the passed in format
    /// </summary>
    /// <param name="list">The item list</param>
    /// <param name="listType">The single-place format string to use</param>
    public static string ToFormattedList(this IEnumerable list, ListType listType)
    {

        var outerListFormat = "";
        var listFormat = "";

        switch (listType)
        {
            case ListType.Ordered:
                outerListFormat = "<ol>{0}</ol>";
                listFormat = "<li>{0}</li>";
                break;
            case ListType.Unordered:
                outerListFormat = "<ul>{0}</ul>";
                listFormat = "<li>{0}</li>";
                break;
            case ListType.TableCell:
                outerListFormat = "{0}";
                listFormat = "<td>{0}</td>";
                break;
            default:
                break;
        }

        return string.Format(outerListFormat, ToFormattedList(list, listFormat));
    }

    /// <summary>
    /// Creates a formatted list of items based on the passed in format
    /// </summary>
    /// <param name="list">The item list</param>
    /// <param name="format">The single-place format string to use</param>
    public static string ToFormattedList(IEnumerable list, string format)
    {

        var sb = new StringBuilder();
        foreach (var item in list)
        {
            sb.AppendFormat(format, item);
        }

        return sb.ToString();
    }

    public static string GetSiteUrl(this ViewPage pg)
    {
        var port = pg.ViewContext.HttpContext.Request.ServerVariables["SERVER_PORT"];
        if (port == null || port == "80" || port == "443")
            port = "";
        else
            port = ":" + port;

        var protocol = pg.ViewContext.HttpContext.Request.ServerVariables["SERVER_PORT_SECURE"];
        if (protocol == null || protocol == "0")
            protocol = "http://";
        else
            protocol = "https://";

        var appPath = pg.ViewContext.HttpContext.Request.ApplicationPath;
        if (appPath == "/")
            appPath = "";

        var sOut = protocol + pg.ViewContext.HttpContext.Request.ServerVariables["SERVER_NAME"] + port + appPath;
        return sOut;
    }

    /// <summary>
    /// Creates a ReturnUrl for use with the Login page
    /// </summary>
    public static string GetReturnUrl(this ViewPage pg)
    {

        var returnUrl = pg.Html.ViewContext.HttpContext.Request.QueryString["ReturnUrl"] ??
                           pg.Html.ViewContext.HttpContext.Request.Url.AbsoluteUri;
        return returnUrl;
    }

    public static bool UserIsAdmin(this ViewPage pg)
    {
        return pg.User.IsInRole("Administrator");
    }

    public static bool UserIsAdmin(this ViewUserControl ctrl)
    {
        return ctrl.Page.User.IsInRole("Administrator");
    }
}